/*Joseph A. Coll
Cleaning 2012 SPAE
9/3/2021 */

use "G:\My Drive\Backup 08202022\Working Papers\Michaels Edited Volume\Election Policies Fraud Beliefs Confidence\Data and Methods\2012 SPAE.dta", clear

tab regstate
rename regstate state_fips
statastates, fips(state_fips)
replace state_name = proper(state_name)
gen year = 2012 
drop _merge

*Voted
tab q1
tab q1, nol
recode q1 (1/5=0 "Did not vote") (6=1 "Voted"), gen(voted)
label var voted Voted
tab voted

*Reason for not voting, second reason
*tab q2

*First time voters
tab q3
tab q3, nol
recode q3 (1 3=1 "First Time Voter") (2=0 "Not First Time Voter") (99=1), gen(voted_first_time)
label var voted_first_time "First Time Voter"

*Mode of voting 
tab q4 
tab q4, nol
recode q4 (1=1 "Voted on Election Day") (2=2 "Voted Prior to Election-Day") ///
(3=3 "Voted Absentee/Mail") (4/99=.), gen(voted_how)
label var voted_how "How Respondent Voted"

recode q4 (1/2=1 "Voted in Person") (3=0 "Voted by Mail/Absentee") (4/99=.), gen(voted_inperson)
label var voted_inperson "Voted In-Person"

recode q4 (1=0 "Voted on Election Day") (2/3=1 "Voted Prior to Election-Day") ///
(4/99=0), gen(voted_earlymail)
label var voted_earlymail "Voted Early/Mail"

recode q4 (1 3=0 "Voted on Election Day/Mail") (2=1 "Voted Early") ///
(4 9/99=0), gen(voted_early)
label var voted_early "Voted Early"

recode q4 (1/2=0 "Did not Vote by Mail") (3=1 "Voted by Mail") ///
(4/99=0), gen(voted_mail)
label var voted_mail "Voted by Mail"

recode q4 (1=1 "Election Day") (2 3=0 "After Election Day") (4/99=0), gen(voted_ed)
label var voted_ed "Voted on Election Day"

*Difficutly Finding Polling Place
tab q5
tab q5, nol
recode q5 (1=4 "Very Difficult") (2=3 "Somewhat Difficult") (3=2 "Fairly Easy") ///
(4=1 "Very Easy") (5/99=1) (.=1), gen(difficulty_finding_poll)
label var difficulty_finding_poll "Difficulty Finding Polling Place"

*Polling place type
tab q6
tab q6, nol
recode q6 (1=1 "Private Business") (2=2 "School") (3=3 "Church") (4=4 "Police/Fire Station") ///
(5=5 "Store/Mall") (6=6 "Senior Center") (7=7 "Community Center") (8=8 "Library") ///
(9=9 "Sports Arena") (10=10 "Gov't Office") (11=11 "Other") (9/999=.), gen(poll_type)
label var poll_type "Type of Polling Place"

*Knew who checked you in
tab q7
tab q7, nol
recode q7 (1=1 "Yes") (2=0 "No") (3/99=.), gen(knew_checkin_worker)
label var knew_checkin "Knew the Check-in Worker"

*Polling place run evaluation
tab q8
tab q8, nol
sum q8 if q8 < 5
recode q8 (1=4 "Very Well") (2=3 "Okay") (3=2 "Not Well") (4=1 "Terrible") ///
(5/99=.), gen(poll_eval4)
label var poll_eval4 "Poll Place Eval."

recode q8 (1=3 "Very Well") (2=2 "Okay") (3/4=1 "Not Well") ///
(5/99=.), gen(poll_eval3)
label var poll_eval3 "Poll Place Eval."

*Voter registration problem
tab q9
tab q9, nol
recode q9 (1=1 "Yes") (2/9=0 "No") (3/99=0) (.=0), gen(reg_problem)
label var reg_problem "Reg. Problems"

*Time voted
tab q9a
tab q9a, nol
recode q9a (1=1 "Before 5 AM") (2=2 "5-6 AM") (3=3 "6-7 AM") (4=4 "7-8 AM") ///
(5=5 "8-9 AM") (6=6 "9-10 AM") (7=7 "10-11 AM") (8=8 "11-12 PM") (9=9 "12-1 PM") ///
(10=10 "1-2 PM") (11=11 "2-3 PM") (12=12 "3-4 PM") (13=13 "4-5 PM") (14=14 "5-6 PM") ///
(15=15 "6-7 PM") (16=16 "7-8 PM") (17=17 "8-9 PM") (18=18 "After 9 PM") (55/999=.) ///
, gen(voted_time18)
labe var voted_time18 "Time Voted"

recode q9a (1/4=1 "Before 8 AM") (5=2 "8-9 AM") (6=3 "9-10 AM") (7=4 "10-11 AM") ///
(8=5 "11-12 PM") (9=6 "12-1 PM") (10=7 "1-2 PM") (11=8 "2-3 PM") (12=9 "3-4 PM") ///
(13=10 "4-5 PM") (14/18=11 "After 5 PM") (9/999=.) ///
, gen(voted_time11)
labe var voted_time11 "Time Voted"

gen voted_time9_12 = 1 if voted_time11 >2 | voted_time11  <6 
replace voted_time9_12 = 0 if voted_time11 <3 | voted_time11 >5
label var voted_time9_12 "Morning Voter"

*Polling place closed
tab q9b
tab q9b, nol
recode q9b (1=0 "Open") (2=1 "Closed") (3/99=.), gen(poll_closed)
label var poll_closed "Polling Place was Closed"


*Wait time
tab q10
tab q10, nol
recode q13 (1=1 "No wait") (2=2 "<10 Min.") (3=3 "10-30 Min.") (4=4 "30-60 Min.") ///
(5=5 ">60 Min.") (6/99=1), gen(wait_time5)
label var wait_time5 "Wait Time"

recode q10 (1=1 "No wait") (2=2 "<10 Min.") (3=3 "10-30 Min.") (4/5 =4 ">30 Min.") ///
(6/99=.), gen(wait_time4)
label var wait_time4 "Wait Time"
tab wait_time4 

*Where wait occurred
tab q11
tab q11, nol
recode q14 (1=1 "Check In") (2=3 "Voting queue") (3=2 "Evenly Split") (4/99=.) ///
, gen(wait_where_happened3)
recode q14 (1=0 "Check In") (2/3=1 "Voting queue") (9/99=.) ///
, gen(wait_where_happened2)
sum wait_where_happened3 wait_where_happened2

*Showed ID
tab q12
tab q12, nol
tab q12 if q12 != 99
recode q12 (1=0 "Gave Name/Address") (2/7=1 "Proved ID") (5 9/99=.), gen(proved_id)
label var proved_id "Proved Identification"

*Asked for ID
tab q13
gen asked_photo  = 1 if q13 ==1 
replace asked_photo  = 0 if asked_photo   != 1  
labe var asked_photo  "Asked for Photo ID"
tab asked_photo 

*Voting machine problem
tab q14
tab q14, nol
recode q14 (1=1 "Yes") (2=0 "No") (3/99=0) (.=0), gen(machine_problem)
label var machine_problem "Voting Problems"
tab machine_problem

*Poll worker evals
tab q15
tab q15, nol
recode q15 (1=4 "Excellent") (2=3 "Good") (3=2 "Fair") (4=1 "Poor") (5/99=.), gen(pollworker_eval4)
label var pollworker_eval4 "Poll Worker Eval."
tab pollworker_eval4

recode q15 (1=3 "Excellent") (2=2 "Good") (3/4=1 "Fair") (5/99=.), gen(pollworker_eval3)
label var pollworker_eval3 "Poll Worker Eval."
tab pollworker_eval3 

/*Poll worker race
tab q16
tab q16, nol
recode q16 (1/3 5/6 9=1 "Non-White Poll Worker") (4=0 "White Poll Worker") ///
(8/99=.), gen(pollworker_race)
label var pollworker_race "Poll Worker Race"
tab pollworker_race
*/

/*Poll worker age
tab q17 
recode q17 (1=1 "<30") (2=2 "31-50") (3=3 "51-70") (4=4 "70+") (9/99=.), gen(pollworker_age)
label var pollworker_age "Poll Worker Age"
*/

*Voted for winner
tab q24, 
tab q24, nol

recode q24 (2=1 "Voted for Winner") (1 3/5=0 "Voted for Loser/No Vote"), gen(voted_for_winner)
label var voted_for_winner "Voted for Winner"
tab voted_for_winner 

*confidence in own vote count
tab q25
tab q25, nol
recode q25 (1=4 "Very Confident") (2=3 "Somewhat Confident") (3=2 "Not too Confident") ///
(4=1 "Not at All Confident") (5/99=.), gen(confidence_ownvote)
label var confidence_ownvote "Confidence Own Vote Counted Correctly"

*confidence in county vote count
tab q26
recode q26 (1=4 "Very Confident") (2=3 "Somewhat Confident") (3=2 "Not too Confident") ///
(4=1 "Not at All Confident") (5/99=.), gen(confidence_countyvote)
label var confidence_countyvote "Confidence Local Votes Counted Correctly"

*confidence in state vote count
tab q27
recode q27 (1=4 "Very Confident") (2=3 "Somewhat Confident") (3=2 "Not too Confident") ///
(4=1 "Not at All Confident") (5/99=.), gen(confidence_statevote)
label var confidence_statevote "Confidence State Votes Counted Correctly"
clonevar confidence_statevote_original = q27 

*confidence in natl vote count
tab q28
recode q28 (1=4 "Very Confident") (2=3 "Somewhat Confident") (3=2 "Not too Confident") ///
(4=1 "Not at All Confident") (5/99=.), gen(confidence_natlvote)
label var confidence_natlvote "Confidence National Votes Counted Correctly"

*Generate Binary Variables
	gen  confidence_ownvote_binary =1 if  confidence_ownvote >3
	replace confidence_ownvote_binary = 0 if confidence_ownvote < 4
	
	gen  confidence_countyvote_binary =1 if  confidence_countyvote >3
	replace confidence_countyvote_binary = 0 if confidence_countyvote < 4
	
	gen  confidence_statevote_binary =1 if  confidence_statevote >3
	replace confidence_statevote_binary = 0 if confidence_statevote < 4
	
	gen  confidence_natlvote_binary =1 if  confidence_natlvote >3
	replace confidence_natlvote_binary = 0 if confidence_natlvote < 4
	

*Fraud
	*Vote multiple times
	tab q29a 
	tab q29a, nol
	recode q29a (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ), gen(fraud_voter)
	label var fraud_voter "Repeat Voting"
	tab fraud_voter
	
	recode q29a (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_voter_binary)
	label var fraud_voter_binary "Repeat Voting"
	tab fraud_voter_binary

	*Steal ballots
	tab q29b 
	tab q29b, nol
	recode q29b (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ), gen(fraud_tamper)
	label var fraud_tamper "Vote Theft"
	tab fraud_tamper

	recode q29b (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_tamper_binary)
	label var fraud_tamper_binary "Vote Theft"
	tab fraud_tamper_binary

	*Impersonation
	tab q29c
	tab q29c, nol
	recode q29c (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ),gen(fraud_impersonation)
	label var fraud_impersonation "Voter Impersonation"
	tab fraud_impersonation

	recode q29c (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_impersonation_binary)
	label var fraud_impersonation_binary "Voter Impersonation"
	tab fraud_impersonation_binary

	*Non-Citizen voters
	tab q29d
	tab q29d, nol
	recode q29d (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ), gen(fraud_noncitizen)
	label var fraud_noncitizen "Non-Citizen Voting"
	tab fraud_noncitizen

	recode q29d (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_noncitizen_binary)
	label var fraud_noncitizen_binary "Non-Citizen Voting"
	tab fraud_noncitizen_binary

	*Illegal absentee 
	tab q29e
	tab q29e, nol
	recode q29e (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ), gen(fraud_wrongabsentee)
	label var fraud_wrongabsentee "Absentee Impersonation"
	tab fraud_wrongabsentee

	recode q29e (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_wrongabsentee_binary)
	label var fraud_wrongabsentee_binary "Absentee Impersonation"
	tab fraud_wrongabsentee_binary

	*Officials change count 
	tab q29f
	tab q29f, nol
	recode q29f (1=5 "Very Common") (2=4 "Occasional") (3=2 "Infrequent") (4=1 "Never") ///
	(5/99 . =3 "Unsure" ), gen(fraud_officialscheat)
	label var fraud_officialscheat "Officials Change Vote Count"
	tab fraud_officialscheat

	recode q29f (1/3 5 . =1 "Frequently") (4=0 "Almost Never"), gen(fraud_officialscheat_binary)
	label var fraud_officialscheat_binary "Absentee Impersonation"
	tab fraud_officialscheat_binary

	*Unsure of fraud indicator
	gen fraud_unsure = 1 if fraud_voter ==3 | fraud_tamper ==3 | ///
	fraud_impersonation ==3 | fraud_noncitizen ==3 | fraud_wrongabsentee ==3 ///
	| fraud_officialscheat==3 
	replace fraud_unsure  = 0 if fraud_unsure  != 1
	tab fraud_unsure  
	tab   fraud_voter fraud_unsure
	tab   fraud_tamper fraud_unsure
	tab   fraud_impersonation fraud_unsure
	tab   fraud_noncitizen fraud_unsure
	tab   fraud_wrongabsentee fraud_unsure
	tab   fraud_officialscheat fraud_unsure

	gen fraud_scale = fraud_voter + fraud_tamper + fraud_impersonation + ///
	fraud_noncitizen + fraud_wrongabsentee + fraud_officialscheat
	replace fraud_scale = fraud_scale - 6
	replace fraud_scale = fraud_scale/6
	label var fraud_scale  "Fraud Belief Scale"
	tab fraud_scale 

	gen voter_fraud_scale = fraud_voter + fraud_impersonation + ///
	fraud_noncitizen + fraud_wrongabsentee 
	tab voter_fraud_scale 
	replace voter_fraud_scale = voter_fraud_scale - 4
	*replace voter_fraud_scale = voter_fraud_scale/4
	replace voter_fraud_scale = voter_fraud_scale/16
	replace voter_fraud_scale = voter_fraud_scale * 100 
	label var voter_fraud_scale  "Voter Fraud Belief Scale"
	tab voter_fraud_scale 

	gen election_fraud_scale =  fraud_tamper + fraud_officialscheat
	tab election_fraud_scale 
	replace election_fraud_scale = election_fraud_scale - 2
	replace election_fraud_scale = election_fraud_scale/2
	label var election_fraud_scale  "Election Fraud Belief Scale"
	tab election_fraud_scale 

	
*Reform proposals
tab q34a 
tab q34a, nol
local vars q34a q34b q34c q34d q34e q34f q34g q34h q34i q34j q34k
foreach x of local vars {
recode `x' (1=3 "Strongly Support") (2=2 "Somewhat Support") (3=1 "Somewhat Oppose") ///
(4=0 "Strongly Oppose") (8 9/99=.) , gen(`x'_recode)
}
 
          
label var q34a_recode "Support for Internet Voting"
label var q34b_recode "Support for Cell-Phone Voting"
label var q34c_recode "Support for All-Mail Elections"
label var q34d_recode "Support for Auto-Register at 18"
label var q34e_recode "Support for Election-Day Registration"
label var q34f_recode "Support for Voter ID"
label var q34g_recode "Support for Paper Back-Up at Machines"
label var q34h_recode "Support for Weekend Election-Day"
label var q34i_recode "Support for Election-Day as Holiday"
label var q34j_recode "Support for Non-Partisan Election Official Selection"
label var q34k_recode "Support for Auto-Register After Moving" 

rename  q34a_recode    support_internet_voting
rename  q34b_recode    support_cellphone_voting
rename  q34c_recode   support_allmail
rename  q34d_recode   support_autoreg_18
rename  q34e_recode    support_edr
rename  q34f_recode    support_vid
rename  q34g_recode    support_paperbackup
rename  q34h_recode   support_weekend_ed
rename  q34i_recode    support_holiday_ed
rename  q34j_recode    support_nonpartisan_leo
rename  q34k_recode    support_autoreg_move

*Residency Length
tab time_1,
tab time_1, nol
recode time_1 (51/99=51 ">50 Years"), gen(residency_length)
labe var residency_length "Residency Length"

*Political interest
tab newsint
recode newsint (1=1 "High Interest") (2/98=0 "Low Interest"), gen(poli_int)
label var poli_int "Political Interest"

*Income
tab income
tab income, nol
recode income (12/16=12) (97/999=.), gen(income_mean)
sum income_mean 
replace income_mean = 6 if income_mean == .
label var income_mean "Income"

*Education
tab educ
tab educ, nol
rename educ educ_precode
recode educ (1/2=1 "HS/Less") (3=2 "Some College") (4=3 "Assoc.") (5=4 "Bach.") ///
(6=5 "Adv.") (99=.), gen(educ)
label var educ Education

*PID
tab pid3 
tab pid3, nol
recode pid3 (1=1 "Democrat") (2=3 "Republican") (3/5=2 "Independent"), gen(pid3_recode)
label var pid3_recode Partisanship
tab pid3_recode

recode pid3 (1=1 "Democrat") (2/5=0 "Not Democrat"), gen(dem)
label var dem Democrat

recode pid3 (2=1 "Republican") (1 3/5=0 "Not Republican"), gen(rep)
label var rep Republican

recode pid3 (3/5=1 "Independent") (1/2=0 "Not Independent"), gen(ind)
label var ind Independent

tab pid7 
tab pid7, nol
recode pid7 (1=1 "Strong Democrat") (2=2 "Moderate Democrat") (3=3 "Lean Democrat") ///
(4 8=4 "Independent") ///
(5=5 "Lean Republican") (6=6 "Moderate Republican") (7=7 "Strong Republican"), gen(pid7_recode)
label var pid7_recode Partisanship
tab pid7_recode

recode pid7 (1=1 "Strong Democrat") (2/8=0 "Not Strong Democrat"), gen(dem_strong) t
label var dem_strong "Strong Democrat"

recode pid7 (2=1 "Moderate Democrat") (1 3/8=0 "Not Moderate Democrat"), gen(dem_mod) t
label var dem_mod "Moderate Democrat"

recode pid7 (3=1 "Lean Democrat") (1 2 4/8=0 "Not Lean Democrat"), gen(dem_lean) t
label var dem_lean "Lean Democrat"

recode pid7 (4 8=1 "Independent") (1/3 5/7=0 "Not Independent"), gen(ind_nolean) t
label var ind_nolean "Independent"

recode pid7 (5=1 "Lean Republican") (1/4 6/7=0 "Not Lean Republican"), gen(rep_lean) t
label var rep_lean "Lean Republican"

recode pid7 (6=1 "Moderate Republican") (1/5 7=0 "Not Moderate Republican"), gen(rep_mod) t
label var rep_mod "Moderate Republican"

recode pid7 (7=1 "Strong Republican") (1/6=0 "Not Strong Republican"), gen(rep_strong) t
label var rep_strong "Strong Republican"

gen rep_w_lean = 1 if pid7_recode >4
replace rep_w_lean = 0 if pid7_recode <5
label var rep_w_lean "Republican"

gen dem_w_lean = 1 if pid7_recode <4
replace dem_w_lean = 0 if pid7_recode >3
label var dem_w_lean "Democrat"

*Ideology
tab ideo5
tab ideo5, nol
recode ideo5 (1=5 "Very Liberal") (2=4 "Liberal") (3=3 "Moderate") ///
(4=2 "Conservative") (5=1 "Very Conservative") (6/99=3) , gen(liberalism)
label var liberalism Liberalism

*race
tab race
recode race (1=1 "White") (2/8=0 "Not White"), gen(white_non_hispanic)
label var white_non_hispanic "White"

recode race (2=1 "Black") (1 3/8=0 "Not Black"), gen(black_non_hispanic)
label var black_non_hispanic "Black"

drop hispanic
recode race (3=1 "Hispanic") (1 2 4/8=0 "Not Hispanic"), gen(hispanic)
label var hispanic "Hispanic"

recode race (4/8=1 "Other") (1/3=0 "Not Other"), gen(other_non_hispanic)
label var other_non_hispanic "Other"
	
*gender
tab gender
recode gender (1=0 "Male") (2=1 "Female"), gen(female)
label var female Female

*Age
tab birthyr
gen age = 2012-birthyr
label var age Age

gen age2 = age * age
label var age2 "Age Squared"

*Marital status
tab marstat
recode marstat (1=1 "Married") (2/6=0 "Not Married"), gen(married)
label var married Married 

*Save
save "G:\My Drive\Backup 08202022\Working Papers\Michaels Edited Volume\Election Policies Fraud Beliefs Confidence\Data and Methods\2012 SPAE Cleaned.dta", replace

sum confidence_ownvote confidence_countyvote confidence_statevote confidence_natlvote /// DV
	      /// COVI
	voted_first_time wait_time4 voted_early reg_problem machine_problem difficulty_finding_poll /// Voting Exp.
	hispanic black_non_hispanic other_non_hispanic /// Race/Ethnicity
	age income_mean educ married female /// SES
	dem rep liberalism poli_int /// Poli Characteristics
	state_fips
	




